<?php

class LogModule extends BaseModule {

	public function __construct() {
		parent::__construct();
	}

	public function pageShow($pageConfig, $type = "list", $controllerName) {
		$table = $pageConfig ['table'];

		$returnList = array();
		$where = array();
		$searchCase = array();
		$wh = "";
		$i = 1;
		foreach ($pageConfig ['selectCase'] as $field => $config) {
			$typeFunctionName = $config ["type"];

			if (isset($config ["parameter"])) {
				$parameter = array();
				foreach ($config ["parameter"] as $para) {
					$parameter [$para] = $_GET [$para];
					$returnList [$para] = $_GET [$para];
				}
				if (!empty($parameter)) {
					$whereSql = LogPageConfig::$typeFunctionName($field, $parameter);
				}
			} else {
				$returnList [$field] = $_GET [$field];
				$whereSql = LogPageConfig::$typeFunctionName($field, $_GET [$field]);
			}
			if ($type == "list") {
				if (isset($config ["searchCase"])) {
					$searchName = $config ['searchName'];
					if (isset($config ["parameter"])) {
						$key = $config ["parameter"];
						$value = array();
						foreach ($config ["parameter"] as $para) {
							$value [$para] = $returnList [$para];
						}
					} else {
						$key = $field;
						$value = $returnList [$field];
					}

					$searchCaseFunctionName = 'searchCase' . $config ["searchCase"];
					if (isset($config ["dbMapping"])) {
						$searchCase [$field] = LogPageConfig::$searchCaseFunctionName($key, $searchName, $config ["dbMapping"], $value);
					} else {
						$searchCase [$field] = LogPageConfig::$searchCaseFunctionName($key, $searchName, $value);
					}

					$searchCase[] = "</li><li>";
// 					if($i % 5 == 0){
// 						$searchCase[] = "</td></tr><tr><td>";
// 					}else{
// 						$searchCase[] = "</td><td>";
// 					}
					$i++;
				}
			}
			if (!empty($whereSql)) {
				$where [] = $whereSql;
			}
		}
		foreach ($pageConfig['filter'] as $filter) {
			$where [] = " {$filter['field']} {$filter['type']} '{$filter['value']}' ";
		}
		if (!empty($where)) {
			$wh = " where ";
			$wh .= implode(' and ', $where);
		}

		if ($type == "list") {
			$pageSize = 30;
			$wh .= " order by id DESC";

			$totle = LogDao::getList($table, "count", 0, 0, $wh);
			// 分页
			$page = new Page($totle [0] ["count"], $pageSize);
			$totalPage = $page->get_totalPages();
			$nowPage = $page->get_nowPage();

			$list = LogDao::getList($table, "limit", ($nowPage - 1) * $pageSize, $pageSize, $wh);

			$pageHtml = $page->show();
		} else {
			$list = LogDao::getList($table, "all", 0, 0, $wh . " order by id DESC");
		}

		$tableTitle = array();
		foreach ($pageConfig['listConfg'] as $key => $value) {
			$tableTitle[] = $key;
		}
		$tableValue = array();
		$newLogId = 1;
		foreach ($list as $listValue) {
			$tableValueTmp = array();
			$trColor = '#FFFFFF';
			foreach ($pageConfig['listConfg'] as $value) {
				if (isset($pageConfig['selectCase'][$value]['dbMapping'])) {
					$tableValueTmp[$value] = $pageConfig['selectCase'][$value]['dbMapping'][$listValue[$value]];
				} else {
					$tableValueTmp[$value] = $listValue[$value];
				}
				if (isset($pageConfig['redTr'])) {
					if ($value == $pageConfig['redTr']['field']) {
						if ($listValue[$value] == $pageConfig['redTr']['value']) {
							$trColor = '#FF5511';
						}
					}
				}
			}
			$tableValueTmp['newLogId'] = $newLogId;
			$newLogId++;
			if ($type == "list" && $pageConfig['showMore'] == true) {
				$tableValueTmp['more'] = "<a href='" . HTTPFILEURL . "/{$controllerName}/{$table}/showMore/?logId={$listValue[$pageConfig['logId']]}' >查看详细</a>";
			}
			$tableValue[] = array('value' => $tableValueTmp, 'trColor' => $trColor);
		}

		$returnList ['tableTitle'] = $tableTitle;
		$returnList ['tableValue'] = $tableValue;
		if ($type == "list") {
			if ($pageConfig['showMore'] == true) {
				$returnList ['tableTitle'][] = "操作";
			}
			$returnList ['pageHtml'] = $pageHtml;
			$returnList ['totle'] = $totle[0]["count"];
			$returnList ['pageName'] = $pageConfig ['pageName'];
			$returnList ['searchCase'] = implode("\n", $searchCase);
		} elseif ($type == "export") {
			$returnList ['table'] = $table;
			LogDao::updateLog($table, ' isExport = 1 ', $wh);
		} elseif ($type == "print") {
			$returnList ['pageName'] = $pageConfig ['pageName'];
		} elseif ($type == "delete") {
			if (empty($wh)) {
				Handler_tool::alertAndClose("不能删除所有数据，请选择查询条件！");
			}
			$returnList ['table'] = $table;
			LogDao::updateLog($table, ' isExport = 1 ', $wh);
			LogDao::deleteLog($table, $wh);
		}

		return $returnList;
	}

	public function moreShow($pageConfig, $logId, $controllerName) {
		$where = "where {$pageConfig['logId']} = {$logId}";
		$logInfo = LogDao::getList($pageConfig ['table'], "all", 0, 0, $where);

		$table = array();
		foreach ($pageConfig['moreConfg'] as $key => $value) {
			if (is_array($value)) {
				if ($value['type'] == 'photo') {
					if (!empty($logInfo[0][$value['value']])) {
						$table[$key] = "<img style='max-width: 940px;height:auto;width:expression(this.width > 940 ? \"940px\" : this.width); ' src='/{$controllerName}/showImg/{$pageConfig ['table']}/{$value['value']}/?logId={$logId}'>";
					}
				} elseif ($value['type'] == 'fileList') {
					$fileList = $logInfo[0][$value['value']];
					$table[$key] = '<table class="newfont03" cellspacing="1" cellpadding="4" border="0" bgcolor="#464646">';
					$table[$key] .= '	<tr>';
// 					$table[$key] .= '		<td align="center" bgcolor="#EEEEEE">编号</td>';
					$table[$key] .= '		<td align="center" bgcolor="#EEEEEE">文件名</td>';
					$table[$key] .= '		<td align="center" bgcolor="#EEEEEE">大小</td>';
					$table[$key] .= '	</tr>';

					$fileArray = explode("<br>", $fileList);
					foreach ($fileArray as $file) {
						$fileInfoArray = explode(" ", $file);
						$fileId = $fileInfoArray[0];
						$fileName = "";
						for ($i = 1; $i < count($fileInfoArray) - 1; $i++) {
							$fileName .= $fileInfoArray[$i];
						}
						$fileSize = $fileInfoArray[count($fileInfoArray) - 1];
						$table[$key] .= '	<tr bgcolor="#FFFFFF">';
// 						$table[$key] .= '		<td align="center">'.$fileId.'</td>';
						$table[$key] .= '		<td align="left">' . $fileName . '</td>';
						$table[$key] .= '		<td align="left">' . $fileSize . '</td>';
						$table[$key] .= '	</tr>';
					}
					$table[$key] .= "</table>";
				}
			} else {
				if (isset($pageConfig['selectCase'][$value]['dbMapping'])) {
					$table[$key] = $pageConfig['selectCase'][$value]['dbMapping'][$logInfo[0][$value]];
				} else {
					$table[$key] = $logInfo[0][$value];
				}
			}
		}
		$returnList['pageName'] = $pageConfig['pageName'] . "查看详细";
		$returnList['data'] = $table;
		return $returnList;
	}

	public function writeLog($table, $data) {
		$fieldList = array();
		$valueList = array();

		foreach ($data as $key => $value) {
			$fieldList [] = $key;
			$valueList [] = $value;
		}

		$fieldSql = implode(',', $fieldList);
		$valueSql = implode(',', $valueList);

		return LogDao::writeLog($table, $fieldSql, $valueSql);
	}

	public function updateLog($table, $data, $where) {
		$updateSql = " ";
		foreach ($data as $key => $value) {
			$updateSql .= $key . " = " . $value . " , ";
		}
		$updateSql = substr($updateSql, 0, -2);

		$whereSql = " WHERE ";
		foreach ($where as $key => $value) {
			$whereSql .= $key . " = " . $value . " and ";
		}
		$whereSql = substr($whereSql, 0, -4);

		$result = LogDao::updateLog($table, $updateSql, $whereSql);

		return $result;
	}

}
